<!--
 * @Description: 历史数据
 * @Author: Ronda
 * @Date: 2022-02-16 09:30:08
 * @LastEditors: Ronda
 * @LastEditTime: 2022-05-23 15:19:45
-->
<template>
  <div>
    <el-row>
      <el-col :span="4">
        <div class="tree">
          <el-input placeholder="输入关键字进行过滤" v-model="filterText">
          </el-input>
          <el-tree @node-click="handler" class="filter-tree" :data="empDate" :props="defaultProps"
            :filter-node-method="filterNode" ref="tree">
          </el-tree>
        </div>
      </el-col>
      <el-col :span="20">
        <div v-if="params.device_id">
          <el-form :inline="true" :model="params">
            <el-form-item>
              <el-date-picker value-format="timestamp" v-model="params.date" type="datetimerange" range-separator="至"
                start-placeholder="开始日期" end-placeholder="结束日期">
              </el-date-picker>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="toSearch">搜索</el-button>
            </el-form-item>
          </el-form>
          <el-table :data="monitors" style="width: 100%">
            <el-table-column prop="insert_time" label="日期" align="center">
              <template v-slot='scope'>
                {{ scope.row.insert_time | fmtDate }}
              </template>
            </el-table-column>
            <el-table-column prop="tsp" label="tsp" align="center">
            </el-table-column>
            <el-table-column prop="pm10" label="pm10" align="center">
            </el-table-column>
            <el-table-column prop="pm25" label="pm2.5" align="center">
            </el-table-column>
            <el-table-column prop="temperature" label="温度" align="center">
            </el-table-column>
            <el-table-column prop="humidity" label="湿度" align="center">
            </el-table-column>
            <el-table-column prop="wind_direction" label="风向" width="300">
            </el-table-column>
            <el-table-column prop="wind_speed" label="风速" align="center">
            </el-table-column>
          </el-table>
          
          <!-- 分页组件 -->
          <el-pagination
            background
            :hide-on-single-page="hideOnSinglePage"
            :current-page="params.page"
            :page-size="params.pageSize"
            layout="prev, pager, next, sizes, total"
            :total="total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :page-sizes="[10, 20, 30, 50]">
          </el-pagination>
        </div>
        <div v-else style="text-align: center;color:red;line-height:50px">请选择设备进行查看</div>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { get } from '@/utils/request'
export default {
  data() {
    return {
      filterText: '',
      empDate: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      params: {
        page: 1,
        pageSize: 10, // 默认每页10条数据
        device_id: 0,
        engineer_id: 0,
        date: []
      },
      monitors: [],
      total: 0,
      hideOnSinglePage: true
    };
  },
  watch: {
    filterText(val) {
      this.$refs.tree.filter(val);
    }
  },

  methods: {
    toSearch() {
      this.params.page = 1; // 搜索时重置到第一页
      this.getMonitor();
    },
    handler(val, node) {
      if (!val.children) {
        this.params.device_id = val.id;
        this.params.engineer_id = val.engineer_id;
        this.params.page = 1; // 切换设备时重置到第一页
        this.getMonitor();
      }
    },
    async getMonitor() {
      if (this.params.date.length > 0) {
        this.params.begin_time = this.params.date[0];
        this.params.end_time = this.params.date[1];
      }
      delete this.params.data;
      let res = await get('/monitor/pageQuery', this.params);
      this.monitors = res.data.list;
      this.total = res.data.total; // 获取总记录数
    },
    // 每页条数改变
    handleSizeChange(val) {
      this.params.pageSize = val;
      this.getMonitor();
    },
    // 当前页改变
    handleCurrentChange(val) {
      this.params.page = val;
      this.getMonitor();
    },
    filterNode(value, data) {
      if (!value) return true;
      return data.name.indexOf(value) !== -1;
    },
    async getEmpDate() {
      let res = await get('/dashboard/findEngineerDeviceTree');
      this.empDate = res.data;
    }
  },
  created() {
    this.getEmpDate();
  }
};
</script>

<style lang="scss" scoped>
.tree {
  height: calc(100vh - 60px);
  border-right: 1px solid #ededed;
  padding-right: 10px;
  margin-right: 10px;
}
</style>